package com.hzqy.service.hqjc;

import com.alibaba.fastjson.JSONObject;
import com.hzqy.commons.utils.SystemConfigUtils;
import com.hzqy.persistence.dao.hqjc.HqjcDao;
import com.hzqy.service.core.GenericServiceBean;
import com.hzqy.web.login.SessionKey;
import com.hzqy.web.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HqjcServiceBean extends GenericServiceBean<PmsUserVo,HqjcDao, Integer>{
	private static final long serialVersionUID = 1L;
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	
	@Resource
	private HqjcDao hqjcDao;

	@Override
	public HqjcDao getEntityDao() {
		return hqjcDao;
	}
	
	public CmCatalogVo selectCatalog(int id) {
		CmCatalogVo results = hqjcDao.selectCatalog(id);
		return results;
	}
	
	public Map<String,Object> selectSeriesInfo(CmContentVo contentVo) {
		Map<String,Object> map = new HashMap<String, Object>();
		if(contentVo.getF_cc_cprid()==null||contentVo.getF_cc_cprid().trim().length()==0) //假设未传取内容图，则默认设置为取5内容详情图
			contentVo.setF_cc_cprid("5");
		CmContentVo content = hqjcDao.selectContent(contentVo);
		int count = hqjcDao.selectSeriesCount(contentVo);
		List<CmSeriesVo> results = hqjcDao.selectAllSeries(contentVo);
		map.put("content", content);
		map.put("count", count);
		map.put("seriesList", results);
		return map;
	}
	
	public List<CmContentVo> selectSeriesListByCatalogId(int id,int pageNo,int pageSize) {
		List<CmContentVo> results = hqjcDao.selectSeriesListByCatalogId(id,pageNo,pageSize);
		return results;
	}
	public int selectCatalogInfoListByParentCatalogIdCount(int f_catalog_id,int f_po_id) {
		int results = hqjcDao.selectCatalogInfoListByParentCatalogIdCount(f_catalog_id,f_po_id);
		return results;
	}
	public List<CmCatalogVo> selectCatalogInfoListByParentCatalogId(int f_catalog_id,int f_po_id,int pageNo,int pageSize) {
		List<CmCatalogVo> results = hqjcDao.selectCatalogInfoListByParentCatalogId(f_catalog_id,f_po_id, pageNo, pageSize);
		return results;
	}
	
	public int selectContentCount(int id,int f_po_id,String f_cp_cprid,CmContentVo contentVo) {
		int results = hqjcDao.selectContentCount(id,f_po_id,f_cp_cprid,contentVo);
		return results;
	}
	
	public int selectContentCountIn(String id,int f_po_id,String f_cp_cprid,CmContentVo contentVo) {
		int results = hqjcDao.selectContentCountIn(id,f_po_id,f_cp_cprid,contentVo);
		return results;
	}
	
	public List<CmContentVo> selectContentListByCatalogId(int f_catalog_id,String f_po_id,int pageNo,int pageSize,String f_cp_cprid,CmContentVo contentVo) {
		List<CmContentVo> results = hqjcDao.selectContentListByCatalogId(f_catalog_id,f_po_id,pageNo,pageSize,f_cp_cprid,contentVo);
		String adminpath=SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.ueditor_admin_platform_path.getKey());
		String webpath=SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.cloud_two_plane_url.getKey());
		try{
			if(adminpath!=null&&webpath!=null&&results.size()>0){
				for(int i=0;i<results.size();i++){
					CmContentVo ccVo=results.get(i);
					if(ccVo!=null){
						String desc=ccVo.getF_cc_desc();
						if(desc!=null&&!"".equals(desc)){
							desc=desc.replaceAll(adminpath, webpath);
							results.get(i).setF_cc_desc(desc);
						}
					}
				}
			}
		}catch(Exception e){
			LOG.error("内容列表详情替换失败",e);
		}
		return results;
	}
	public List<CmContentVo> selectContentListByCatalogIdIn(String f_catalog_id,int f_po_id,int pageNo,int pageSize,String f_cp_cprid,CmContentVo contentVo) {
		List<CmContentVo> results = hqjcDao.selectContentListByCatalogIdIn(f_catalog_id,f_po_id,pageNo,pageSize,f_cp_cprid,contentVo);
		String adminpath=SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.ueditor_admin_platform_path.getKey());
		String webpath=SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.cloud_two_plane_url.getKey());
		try{
			if(adminpath!=null&&webpath!=null&&results.size()>0){
				for(int i=0;i<results.size();i++){
					CmContentVo ccVo=results.get(i);
					if(ccVo!=null){
						String desc=ccVo.getF_cc_desc();
						if(desc!=null&&!"".equals(desc)){
							desc=desc.replaceAll(adminpath, webpath);
							results.get(i).setF_cc_desc(desc);
						}
					}
				}
			}
		}catch(Exception e){
			LOG.error("内容列表详情替换失败",e);
		}
		return results;
	}
	public List<Map<String, Object>> selectContentListByCatalogIds(int f_catalog_id,int f_po_id,int count,CmContentVo contentVo) {
		List<CmContentVo> results = hqjcDao.selectContentListByCatalogIds(f_catalog_id,f_po_id,contentVo);
		String defaultImg = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.gd_page_catalog_default_img.getKey());
		String cloudTwoPlaneUrl = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.cloud_two_plane_url.getKey());
		String linkurl = "";
		String imgurl = "";
		List<Map<String, Object>> contentLis=new ArrayList<Map<String, Object>>();
		for(int k=0 ; k<count ; k++){
			Map<String, Object> voMap=new HashMap<String, Object>();
			if(k >= results.size()){
				voMap.put("id", "");
				voMap.put("title", "");
				voMap.put("linkurl", "");
				voMap.put("imgurl", cloudTwoPlaneUrl+defaultImg);
			}else{
				CmContentVo cs=results.get(k);
				linkurl = cs.getF_cc_desc().replace("&amp;","&" ).replaceAll("\\&[a-zA-Z]{0,9};", "").replaceAll("<[^>]*>", "");
				if(linkurl.indexOf("?") == -1){
					linkurl += "?tzlj="+f_catalog_id+"sort"+(k+1)+"&sdyCatalogId="+f_catalog_id;
				}else{
					linkurl += "&tzlj="+f_catalog_id+"sort"+(k+1)+"&sdyCatalogId="+f_catalog_id;
				}
				imgurl = cs.getF_cc_picurl();
				/*if("null".equals(linkurl) || "".equals(linkurl) || null == linkurl || "null".equals(imgurl) || "".equals(imgurl) || null == imgurl){
					linkurl = "";
					imgurl =cloudTwoPlaneUrl + defaultImg;
				}*/
				
				voMap.put("id", cs.getF_cc_id());
				voMap.put("title", cs.getF_cc_name());
				voMap.put("linkurl", linkurl);
				voMap.put("imgurl", cloudTwoPlaneUrl + imgurl);
			}
			contentLis.add(voMap);
		}
		return contentLis;
	}
	public List<CmCatalogVo> selectCatalogListByCatalogParentId(int f_catalog_parentid ,int f_po_id ,CmContentVo contentVo) {
		List<CmCatalogVo> results = hqjcDao.selectCatalogListByCatalogParentId(f_catalog_parentid);
		return results;
	}
	public String catalog_list_gd(int f_catalog_parentid ,int f_po_id,String callback, CmContentVo contentVo) {
		List<CmCatalogVo> results = selectCatalogListByCatalogParentId(f_catalog_parentid,f_po_id,contentVo);
		String retCode="";
		String retMsg="";
		List<Map<String, Object>> contents=new ArrayList<Map<String, Object>>();
		Map<String, Object> resultMap=new HashMap<String, Object>();
		if(results.size()<=0){
			retCode="1212202";
			retMsg="数据库无对应模块";
		}else{
			int id ;
			int count;
			for(int i=0;i<results.size();i++){
				id=results.get(i).getF_catalog_id();
				count=Integer.parseInt(results.get(i).getF_catalog_url());
				List<Map<String, Object>> contentList = selectContentListByCatalogIds(id,f_po_id,count,contentVo);
				
				Map<String, Object> map=new HashMap<String, Object>();
				map.put("total", contentList.size());
				map.put("moduleCode",results.get(i).getF_catalog_desc());
				map.put("moduleName",results.get(i).getF_catalog_name());
				map.put("vods", contentList);
				contents.add(map);
				if(contentList.size()<=0){
					retCode="1212204";
					retMsg="某模块无对应内容";
				}else if(contentList.size()<count){
					retCode="1212203";
					retMsg="数据库无对应内容";
				}
			}
		}
		if("".equals(retCode)){
			retCode="1212200";
			retMsg="业务操作成功";
		}
		
		if( "".equals(callback) || "null".equals(callback) ||  null == callback ){
			retCode="1212201";
			retMsg="请求参数空值或不足";
			callback="";
		}
		resultMap.put("retCode", retCode);
		resultMap.put("retMsg", retMsg);
		
		JSONObject jsonObj = new JSONObject();
		String result="";
		jsonObj.put("result", resultMap);
		jsonObj.put("contents", contents);
		result = jsonObj.toJSONString();
		
		// result = callback + "(" + result + ")";
		return result;
	}
	
	public CmContentVo selectContent(CmContentVo contentVo) {
		if(contentVo==null) {
			return new CmContentVo();
		} else {
			if(contentVo.getF_cc_cprid()==null||contentVo.getF_cc_cprid().trim().length()==0) //假设未传取内容图，则默认设置为取5内容详情图
				contentVo.setF_cc_cprid("5");
			CmContentVo results = hqjcDao.selectContent(contentVo);
			String adminpath=SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.ueditor_admin_platform_path.getKey());
			String webpath=SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.cloud_two_plane_url.getKey());
			if(adminpath!=null&&webpath!=null&&results!=null){
				try{
					String desc=results.getF_cc_desc();
					if(desc!=null&&!"".equals(desc)){
						desc=desc.replaceAll(adminpath, webpath);
						results.setF_cc_desc(desc);
					}
				}catch(Exception e){
					LOG.error("内容详情替换失败",e);
				}
			}
			return results;
		}
	}
	
	public int selectSeriesCount(CmContentVo contentVo) {
		int results = hqjcDao.selectSeriesCount(contentVo);
		return results;
	}
	
	public List<CmSeriesVo> selectSeriesList(int id,int pageNo,int pageSize) {
		List<CmSeriesVo> results = hqjcDao.selectSeriesList(id,pageNo,pageSize);
		return results;
	}
	
	public CmSeriesVo selectSeries(int id) {
		CmSeriesVo results = hqjcDao.selectSeries(id);
		return results;
	}
	
	public int selectContentIDByCode(String f_cc_code) {
		int results = hqjcDao.selectContentIDByCode(f_cc_code);
		return results;
	}
	
	public List<CmContentVo> selectContentListByCatalogIdRqph(int f_catalog_id,int f_po_id,int pageNo,int pageSize,String f_cp_cprid,CmContentVo contentVo) {
		List<CmContentVo> results = hqjcDao.selectContentListByCatalogIdRqph(f_catalog_id,f_po_id,pageNo,pageSize,f_cp_cprid,contentVo);
		return results;
	}
	
	public int selectGlobalContentListCount(String f_po_id, CmContentVo contentVo) {
		if(contentVo!=null) {
			contentVo.setF_cco_orgId(f_po_id);
			if(contentVo.getF_cc_searchname()!=null && contentVo.getF_cc_searchname().trim().length()>0) { //判断为搜索内容名称
				return hqjcDao.selectGlobalContentListCount(contentVo);
			} else { //判定搜索表演者
				return hqjcDao.selectGlobalContentListByCastSearchNameCount(contentVo);
			}
		} else {
			return 0;
		}
	}
	
	public List<CmContentVo> selectGlobalContentListByActorSearchName(String f_po_id, CmContentVo contentVo) {
		if(contentVo!=null) {
			contentVo.setF_cco_orgId(f_po_id);
			return hqjcDao.selectGlobalContentListByActorSearchName(contentVo);
		} else {
			return new ArrayList<CmContentVo>();
		}
	}
	
	
	public List<CmContentVo> selectGlobalContentList(String f_po_id, CmContentVo contentVo) {
		if(contentVo!=null) {
			contentVo.setF_cco_orgId(f_po_id);
			if(contentVo.getF_cc_searchname()!=null && contentVo.getF_cc_searchname().trim().length()>0) { //判断为搜索内容名称
				return hqjcDao.selectGlobalContentList(contentVo);
			} else { //判定搜索表演者
				return hqjcDao.selectGlobalContentListByCastSearchName(contentVo);
			}
		} else {
			return new ArrayList<CmContentVo>();
		}
	}

	public List<CmCatalogVo> selectCatalogListByContent(CmContentVo contentVo) {
		if(contentVo!=null) {
			return hqjcDao.selectCatalogListByContent(contentVo);
		}
		return null;
	}
	
	public List<CmContentVo> selectGlobalContentListBytags(String f_po_id,CmContentVo contentVo){
		if(contentVo!=null){
			contentVo.setF_cco_orgId(f_po_id);
			return hqjcDao.selectGlobalContentListByTags(contentVo);
		}else{
			return new ArrayList<CmContentVo>();
		}
		
	}
	public PmsOrgVo selectPmsOrgByContentName(String serverId){
		return hqjcDao.selectPmsOrgByContentName(serverId);
	}

	public CmCatalogVo selectParentCatalogById(int f_cc_id) {
		return hqjcDao.selectParentCatalogById(f_cc_id);
	}
	
	public String selectIdsByOrgParentId(PmsOrgVo pmsOrgVo){
		return hqjcDao.selectIdsByOrgParentId(pmsOrgVo);
	}

	/**
	 * 智联学网上课堂数据获取
	 * @param vo
	 * @return
	 */
	/*public Object requestInterfaceData(OnlineClassRoomParamsVo vo) {
		Map res = new HashMap();
		try{
			String zxbhonlinecr = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.zlx_online_classroom_interface_url.getKey());// 智联学网上课堂数据请求接口地址
			String zlxinterface_key = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.zlx_online_classroom_interface_key.getKey());
			String zlxinterface_secret = SystemConfigUtils.getSysConfig(SessionKey.SESSION_SYSTEM_CONFIG.zlx_online_classroom_interface_secret.getKey());
			String timestamp = Long.toString(System.currentTimeMillis());//时间戳
			String sign = SignUtil.sdkSign(zlxinterface_key,zlxinterface_secret,timestamp);//加密签名
			LOG.debug("加密签名:"+sign);
			String params = vo.getParams();
			params = URLEncoder.encode(params, "utf8");
			LOG.debug("编码参数:"+params);
			String pars = "key="+zlxinterface_key + "&secret="+zlxinterface_secret+"&time="+timestamp+"&sign="+sign+"&params="+params;
			res = HttpsUtil.doPost(zxbhonlinecr,pars);
			//res = HttpUtil.post(zxbhonlinecr,pars);
			//log.debug("智联学网上课堂接口请求结果："+res);
		}catch (Exception e){
			LOG.error("智联学网上课堂数据获取失败：",e);
		}
		return res;
	}*/
}
